function checkStatus (response) {
  if (response.status >= 200 && response.status < 300) {
    return response
  } else if (response.status === 401) {
    console.log('401 auth error')
  } else {
    var error = new Error(response.statusText)
    error.response = response
    throw error
  }
}

function parseJSON (response) {
  const body = response.json()
  return body
}

export default function request (url, options, body) {
  let opt = options || {}
  return fetch(url, {opt})
    .then(checkStatus)
    .then(parseJSON)
    .then((data) => (body(data)))
    .catch((err) => (err))
}
